package com.heima.article.controller.v1;


import com.heima.apis.article.AutomaticApi;
import com.heima.article.feign.WemediaFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.dto.WmNewsPageReqDto;
import com.heima.model.media.pojos.WmNews;
import com.heima.article.service.AutomaticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/automatic")
public class AutomaticController implements AutomaticApi {

    @Autowired
    AutomaticService automaticService;
    /**
     * 通过id自动审核文章
     * @param id
     * @return
     */
    @Override
    @GetMapping("/auto/{id}")
    public ResponseResult autoScanByMediaNewsId(@PathVariable("id") Integer id) {


        return automaticService.autoScanByMediaNewsId(id);
    }

    @Autowired
    WemediaFeign wemediaFeign;

    /**
     * 自媒体文章人工审核
     * id查询
     * @param id
     * @return
     */
    @Override
    @GetMapping("/get/{id}")
    public ResponseResult get(@PathVariable("id") Integer id) {
        return wemediaFeign.get(id);
    }

    /**
     * 通过id修改文章，通过或驳回
     * @param wmNews
     * @return
     */
    @Override
    @PutMapping("/updateWmNews")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {

        ResponseResult result = wemediaFeign.updateWmNews(wmNews);
        //判断状态是否为通过，如果为4则自动审核，进行保存
        if (wmNews.getStatus()==WmNews.Status.ADMIN_SUCCESS.getCode()||result.getCode()== AppHttpCodeEnum.SUCCESS.getCode()){
            automaticService.autoScanByMediaNewsId(wmNews.getId());
        }
        return result;
    }


    /**
     * 分页带条件查询自媒体文章列表
     * @param
     * @return
     */
    @Override
    @PostMapping("/findAll")
    public ResponseResult<List<WmNews>> findAll(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        if (wmNewsPageReqDto.getStatus()==null){
            wmNewsPageReqDto.setStatus(WmNews.Status.ADMIN_AUTH.getCode());
        }
        return wemediaFeign.findAll(wmNewsPageReqDto);
    }
}
